import java.util.*;

public class BinaryTree {

    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }
//    public TreeNode root;

    /**
     * 创建二叉树，创建成功后返回根节点
     */

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        E.right = H;
        C.left = F;
        C.right = G;

        return A;
    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    public void preOrderNor(TreeNode root){
        if (root == null){
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;
        while(cur != null){
            while(cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.peek();
            if(top.right == null || top.right == prev){
                System.out.print(top.val+" ");
                stack.pop();
                prev = top;
            }else {
                cur = top.right;
            }
        }
    }

    //接收前序遍历的结果
    public List<Character> preorderTraveersal(TreeNode root) {
        List<Character> list = new ArrayList<>();
        if (root == null) {
            return null;
        }
        list.add(root.val);
        List<Character> leftTree = preorderTraveersal(root.left);
        list.addAll(leftTree);
        List<Character> rightTree = preorderTraveersal(root.right);
        list.addAll(rightTree);
        return list;
    }

    // 中序遍历
    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    // 后序遍历
    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    public int nodeSize;

    //求树的节点个数
    public int size(TreeNode root) {
        if (root == null) {
            return 0;
        }
        nodeSize++;
        size(root.left);
        size(root.right);
        return nodeSize;
    }

    //求树的节点个数
    public int size2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return size2(root.left) + size2(root.right) + 1;
    }

    //求叶子结点的个数
    public int leafSize;

    public int getLeafnodeCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafnodeCount(root.left);
        getLeafnodeCount(root.right);
        return leafSize;
    }

    public int getLeafnodeCount2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafnodeCount2(root.left) + getLeafnodeCount2(root.right);
    }

    //获取第k层节点的个数
    public int gerKLevelNodeCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return gerKLevelNodeCount(root.left, k - 1) + gerKLevelNodeCount(root.right, k - 1);
    }

    //获取二叉树的高度
    public int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getHeight(root.left) + getHeight(root.right);
    }

    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHeight = getHeight(root.left);
        if(leftHeight < 0){
            return -1;
        }
        int rightHeight = getHeight(root.right);
        if(leftHeight > 0 && rightHeight > 0 && Math.abs(leftHeight-rightHeight) < 1){
            return Math.max(leftHeight,rightHeight)+1;
        }else{
            return -1;
        }
    }

    //检测值为value的元素是否存在
    TreeNode find(TreeNode root, char val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        TreeNode ret1 = find(root.left, val);
        if(ret1 != null){
            return ret1;
        }
        TreeNode ret2 = find(root.right, val);
        if(ret2 != null){
            return ret2;
        }
        return null;
    }

    public boolean isSameTree(TreeNode root, TreeNode subRoot){
        if(root == null && subRoot != null || root != null && subRoot == null){
            return false;
        }
        //上述代码走完以后 ，要么两个都为空，要么两个都不为空
        if(root == null && subRoot == null){
            return true;
        }
        //两个都不为空
        if(root.val != subRoot.val){
            return false;
        }
        return isSameTree(root.left,subRoot.left) && isSameTree(root.right,subRoot.right);
    }

    //判断子树
    public boolean isSubTree(TreeNode root, TreeNode subRoot){
        if(root == null || subRoot == null){
            return false;
        }
        if(isSameTree(root,subRoot)){
            return true;
        }
        if(isSubTree(root.left,subRoot)){
            return true;
        }
        if(isSubTree(root.right,subRoot)){
            return true;
        }
        return false;
    }

    //翻转二叉树
    public TreeNode inverTree(TreeNode root){
        if(root == null){
            return null;
        }
        if(root.left != null && root.right != null){
            TreeNode tmp = root.left;
            root.left = root.right;
            root.right = tmp;
        }
        inverTree(root.left);
        inverTree(root.right);
        return root;
    }

    //判断平衡二叉树
    public boolean isBalanced(TreeNode root){
        if(root == null){
            return true;
        }
        return maxDepth(root) >= 0;
    }

    //判断对称
    public boolean isSymmetric(TreeNode root){
        if(root == null){
            return true;
        }
        return isSymmetricChild(root.left,root.right);
    }
    public boolean isSymmetricChild(TreeNode leftTree, TreeNode rightTree){
        if(leftTree == null && rightTree != null || leftTree != null && rightTree == null){
            return false;
        }
        if(leftTree == null && rightTree == null){
            return true;
        }
        if(leftTree.val != rightTree.val){
            return false;
        }
        return isSymmetricChild(leftTree.left,rightTree.right) && isSymmetricChild(leftTree.right,rightTree.left);
    }

    //层序遍历
    public void levelOrder(TreeNode root){
        if(root == null){
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }
    public List<List<Character>> levelOrder2(TreeNode root){
        List<List<Character>> ret = new ArrayList<>();
        if(root == null){
            return ret;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            int size = queue.size();
            List<Character> tmp = new ArrayList<>();
            while (size != 0) {
                TreeNode cur = queue.poll();
                tmp.add(cur.val);
                size--;
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
            }
            ret.add(tmp);
            //ret.add(0,tmp);
        }
        return ret;
    }

    //判断一棵树是不是完全二叉树
    public boolean isCompleteTree(TreeNode root){
        if(root == null){
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if(cur != null){
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else{
                break;
            }
        }
        //需要判断队列当中是否有非空的元素
        while(!queue.isEmpty()){
            //一个一个元素出队来判断是不是空
            TreeNode tmp = queue.peek();
            if(tmp == null){
                queue.poll();
            }else{
                return false;
            }
        }
        return true;
    }

    //寻找最近的公共祖先
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q){
        if (root == null) {
            return null;
        }
        if(root == p || root == q){
            return root;
        }
        TreeNode leftTree = lowestCommonAncestor(root.left,p,q);
        TreeNode rightTree = lowestCommonAncestor(root.right,p,q);
        if(leftTree != null && rightTree != null){
            return root;
        } else if (leftTree != null) {
            return leftTree;
        }else {
            return rightTree;
        }
    }
    public TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q){
        if(root == null) {return null;}
        Stack<TreeNode> stackP = new Stack<>();
        Stack<TreeNode> stackQ = new Stack<>();
        getPath(root,p,stackP);
        getPath(root,q,stackQ);
        int sizeP = stackP.size();
        int sizeQ = stackQ.size();
        if(sizeP > sizeQ){
            int size = sizeP - sizeQ;
            while(size != 0){
                stackP.pop();
                size--;
            }
        }else {
            int size = sizeQ - sizeP;
            while(size != 0) {
                stackQ.pop();
                size--;
            }
        }
        while(!stackP.isEmpty() && !stackQ.isEmpty()){
            if(stackP.peek() == stackQ.peek()){
                return stackP.peek();
            }else {
                stackP.pop();
                stackQ.pop();
            }
        }
        return null;
    }

    //获取从根节点到 p 或 q 路径上所有节点
    public boolean getPath(TreeNode root, TreeNode node, Stack<TreeNode> stack){
        if (root == null || node == null){
            return false;
        }
        stack.push(root);
        if(root == node){
            return true;
        }
        boolean flg = getPath(root.left,node,stack);
        if(flg == true){
            return true;
        }
        boolean flg2 = getPath(root.right,node,stack);
        if (flg2 == true){
            return true;
        }
        stack.pop();
        return false;
    }

    //前序非递归遍历创建二叉树
//    public int preIndex;
//    public TreeNode buildTree(int[] preOrder, int[] inOrder){
//        return buildTreeChild(preOrder,0,inOrder,0,inOrder.length-1);
//    }
//    public TreeNode buildTreeChild(int[] preOrder, int preIndex, int[] inOrder, int inbegin, int inend){
//        if(inbegin > inend){
//            return null;
//        }
//        TreeNode root = new TreeNode(preOrder[preIndex]);
//        int rootIndex = findIndexRoot(inOrder,inbegin,inend,preOrder[preIndex]);
//        preIndex++;
//        root.left = buildTreeChild(preOrder,preIndex,inOrder,inbegin,rootIndex-1);
//        root.right = buildTreeChild(preOrder,preIndex,inOrder,rootIndex-1,inend);
//        return root;
//    }
//    public int findIndexRoot(int[] inOrder,int inbegin, int inend,int key){
//        for (int i=inbegin;i<= inend;i++){
//            if(inOrder[i] == key){
//                return i;
//            }
//        }
//        return -1;
//    }

    //===================力扣刷题==================
    //判断平衡二叉树
//    public boolean isBalanced2(TreeNode root) {
//        return getHeight(root)>=0;
//    }
//    public int getHeight(TreeNode root){
//        if(root == null){
//            return 0;
//        }
//        int leftHeight = getHeight(root.left);
//        int rightHeight = getHeight(root.right);
//        if (leftHeight == -1 || rightHeight == -1 || Math.abs(leftHeight - rightHeight) > 1) {
//            return -1;
//        } else {
//            return Math.max(leftHeight, rightHeight) + 1;
//        }
//    }


}


